<?php

interface iFrank {

    //methods
    static function getObject($row);
}

class Frank {

    //properties
    protected $id;
    protected $name;

    //getters
    public function id() {
        return $this->id;
    }

    public function name() {
        return $this->name;
    }

    //private methods
    private static function mysqliSelect($queryStr, $db = null) {
        $resultTab = array('query' => $queryStr, 'rows' => null);
        $mysqli = null;
        if ($db == null)
            $mysqli = self::mysqliConnect();
        else
            $mysqli = $db;
		if($mysqli != null){
			$result = $mysqli->query($queryStr);
			if ($result) {
				$resultTab['rows'] = array();
				while ($row = $result->fetch_assoc()) {
					array_push($resultTab['rows'], $row);
				}
				$result->free();
			}
			if ($db == null)
				$mysqli->close();
			return $resultTab;
		}
		return null;
    }

    private static function tabObjectsToArray($myObjects, $toArray = false, $fields = null) {
        if ($toArray) {
            if (isset($myObjects) && count($myObjects) > 0) {
                $myObjectsTab = array();
                foreach ($myObjects as $myObject) {
                    array_push($myObjectsTab, $myObject->toArray($fields));
                }
                return $myObjectsTab;
            }
        }
        return $myObjects;
    }

    private static function filterFields($objectArr, $propertyArray = null) {
        if ($propertyArray != null && count($propertyArray) > 0) {
            $objectSpecArr = array();
            foreach ($propertyArray as $property) {
                if (array_key_exists($property, $objectArr))
                    $objectSpecArr[$property] = $objectArr[$property];
            }
            $objectArr = $objectSpecArr;
        }
        return $objectArr;
    }

    private function cleanObject() {
        foreach ($this as $key => $value) {
            if (is_string($value)) {
                //$this->$key = htmlspecialchars($value);
            }
        }
    }
	
	//protected methods
    protected static function mysqliConnect() {
        $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PWD, DB_NAME);
        if ($mysqli->connect_errno) {
            printf("MySQL connection failure: " . $mysqli->connect_error);
        } else {
            if (!$mysqli->set_charset("utf8"))
                printf("Error loading character set utf8: %s\n", $mysqli->error);
            else
                return $mysqli;
        }

        return null;
    }

    protected static function getFromQuery($query, $db = null) {
        $result = self::mysqliSelect($query, $db);
        if ($result['rows'] != null && count($result['rows']) > 0)
            return $result['rows'];
        return null;
    }
	
    protected static function get($query, $db = null) {
        $results = self::getFromQuery($query, $db);
        if ($results != null) {
            $myObjects = array();
            foreach ($results as $row) {
                $class = get_called_class();
                $newObject = $class::getObject($row); //eval("return new $class($params);");
                array_push($myObjects, $newObject);
            }
            return $myObjects;
        }
        return null;
    }
    protected static function getAll($toArray = false, $fields = null, $query = null, $db = null) {
        $class = get_called_class();
        return self::tabObjectsToArray($class::get($query, $db), $toArray, $fields);
    }

    protected static function getById($id, $toArray = false, $fields = null, $query = null, $db = null) {
        $class = get_called_class();
        return self::tabObjectsToArray($class::get($query, $db), $toArray, $fields);
    }

    /* Get on object to array
     * Posibility to specifiy wich property can appear
     * 
     */
    protected function toArray($propertyArray = null) {
        $this->cleanObject();
        $objectArr = get_object_vars($this);
        return self::filterFields($objectArr, $propertyArray);
    }

    //public methods

    public static function getAllToJson($fields = null, $callback = null) {
        $json['currentDate'] = time();
        $class = get_called_class();
        if ($fields)
            $fields = explode(',', $fields);
        if(isset($callback))
            $objectTab = call_user_func($callback, true, $fields);
        else
            $objectTab = $class::getAll(true, $fields);
        if (isset($objectTab) && count($objectTab) > 0)
            $json[$class] = $objectTab;

        return json_encode($json);
    }

    public static function getByIdToJson($id, $fields = null, $callback = null) {
        $json['currentDate'] = time();
        $class = get_called_class();
        if ($fields)
            $fields = explode(',', $fields);
        if(isset($callback))
            $objectTab = call_user_func($callback, $id, true, $fields);
        else
            $objectTab = $class::getById($id, true, $fields);
        if (isset($objectTab) && count($objectTab) > 0)
            $json[$class] = $objectTab[0];

        return json_encode($json);
    }
}

?>